home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-01-12 | 3.2 KB | 88 lines | [TEXT/GEOL] |
- Item forwarded by LEFFLER1 to RODSETH.R
-
- Item 6564123 11-Jan-90 10:32
-
- From: ROSENSTEIN1 Rosenstein, Larry
-
- To: CPLUS.APPLE$ C++ Interest List--Apple Employees
- UK0310 Paul G Smith
- CPLUS.DEV$ C++ Interest List--Developers
-
- Sub: RE>C++ & ObjectPascal eff
-
- Attn: C++ Apple
- Attn: Paul G Smith
- Attn: C++ Public
- SentBy: Larry Rosenstein
- Date 1/11/90
- Subject RE>C++ & ObjectPascal effic
- From Larry Rosenstein
- To C++ Apple
- Paul G Smith
- C++ Public
-
- Reply to: RE>C++ & ObjectPascal efficien
- In Object Pascal a method call takes 4 bytes. The same is true of a C++
- method call that involves a PascalObject.
-
- In other C++ method calls, however, the call is expanded inline. This means
- that dispatching is very fast.
-
- It also means that the code to index into the dispatch table and adjust the
- object pointer (in the case of multiple inheritance) is duplicated for each
- method call. Each method calls can take as much as 20 bytes.
-
- You can reduce this by using SingleObjects, which uses the C++ 1.2 dispatching
- scheme that doesn't allow for multiple inheritance. The number of bytes is
- still going to be larger than 4.
-
- There's also potential inefficiencies depending on how you use C++. If you
- use stack based objects, then there's extra overhead for constructors and
- destructors that one might not realize at first.
-
- Just declaring a variable:
- TMyObject anObj;
- results in a constructor call, and a destructor call. If the destructor is
- virtual, then you have the virtual call overhead.
-
- If you next do:
- anObj = Func(...);
- then the result of Func is assigned to anObj. In Object Pascal, this would
- assign one handle to another. With a stack based C++ object, the object
- returned by Func is copied into anObj.
-
- The default is to assign each field of the object individually, but any class
- can override the assignment operator it is chooses. If a field is itself an
- object (not an object pointer or reference) then the rule is again applied.
-
- In the above case, you can eliminate the first contructor by constructing and
- initializing the object in 1 step:
- TMyObject anObj = Func(...);
-
- Even better is:
- TMyObject anObj(...);
- where you pass initialization data to the constructor. This eliminated the
- copy entirely.
-
- Objects are also copied if you pass an object by value or return an object
- from a function call. If the parameter (or return result) is declared as
- "TMyObject" then a copy is done; if it is declared as "const TMyObject&" then
- the copy isn't done. Sometimes you want to copy objects, but in many cases it
- isn't necessary.
-
- Finally, it is true that C++ generates bad code in some cases. Part of this
- is a lack of optimizations in MPW C and part is due to the fact that CFront
- generates excessive C code. I think the nature of the compilation process
- (where CFront generates C code that is then compiled) makes it more difficult
- to produce high quality code.
-
- Before placing all the blame on the compilers, it is important to examine the
- source code for inefficiencies.
-
- Larry Rosenstein
-
-
-
-
-
-